<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./art-template(1).js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            background-color: #fff;
            border-radius: 5px;
            width: 1000px;
            margin: 20px auto;
        }

        li {
            list-style: none;
        }

        ul>li {
            height: 60px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #dedede;
            position: relative;
        }

        li img {
            width: 35px;
            height: 35px;
            display: block;
            margin: 0 10px;
        }

        li p {
            color: #333;
        }

        li p>span:first-child {
            color: blueviolet;
        }

        li p>span:last-child {
            color: #999;
            font-size: 14px;
        }

        li>span {
            display: block;
            width: 40px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border-radius: 5px;
            background-color: #999;
            margin: 0 10px;
        }

        li>span.top {
            background-color: aqua;
        }

        h3 {
            font-weight: 400;
        }

        h3>a {
            color: #333;
            text-decoration: none;
        }

        h3>a:hover {
            text-decoration: underline;
        }

        time {
            position: absolute;
            right: 10px;
            top: 20px
        }
    </style>
</head>

<body>
    <div class="box">

    </div>
    <script id="con" type="text/html">
        <ul>
            {{each data as item}}
            <li>
                <img src={{item.author.avatar_url}} alt="">
                <p>
                    <span>{{item.reply_count}}</span>
                    /
                    <span>{{item.visit_count}}</span>
                </p>
                {{if item.top}}
                        <span class="top">置顶</span>
                {{else if item.good}}
                <span class="top">精华</span>
                {{else if item.tab == 'share'}}
                <span>分享</span>
                {{else}}
                <span>问答</span>
                {{/if}}
                <h3><a href="">{{item.title}}</a></h3>

                <time>{{item.last_reply_at|time}}</time>
            </li>
            {{/each}}
        </ul>
    </script>
    <script>
        template.helper("time", function (lastTime) {
            var time1 = new Date().getTime(); //获取当前时间的时间戳
            var time2 = new Date(lastTime).getTime(); //获取参数传递的时间的时间戳
            var cha = time1 - time2; //得到时间戳的差
            var y = parseInt(cha / 1000 / 60 / 60 / 24 / 30 / 12);
            var m = parseInt(cha / 1000 / 60 / 60 / 24 / 30 % 12);
            var d = parseInt(cha / 1000 / 60 / 60 / 24 % 30);
            var h = parseInt(cha / 1000 / 60 / 60 % 24);
            var mi = parseInt(cha / 1000 / 60 % 60);
            if (y) {
                return y + "年前"
            } else if (m) {
                return m + "月前"
            } else if (d) {
                return d + "天前"
            } else if (h) {
                return h + "小时前"
            } else if (mi) {
                return mi + "分钟前"
            } else {
                return "刚刚";
            }
        })

        fetch('https://cnodejs.org/api/v1/topics').then((data) => {
            return data.json();
        }).then(function (res) {
            console.log(res.data[0]);
            var str = template('con', res);
            document.querySelector('.box').innerHTML = str;
        })
    </script>
</body>

</html>